package com.yikome.monitor.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yikome.constant.CommonConstant;
import com.yikome.monitor.request.MonitorQueryRequest;
import com.yikome.monitor.service.MonitorService;
import com.itmuch.lightsecurity.annotation.PreAuthorize;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/monitor")
public class MonitorController {

	@Autowired
	private MonitorService monitorService ; 
	
	@GetMapping("/product")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop', 'affiliate')")
	public Page<Map<String , Object>> findProductMonitor(MonitorQueryRequest monitorQueryRequest, HttpServletRequest request , @PageableDefault(sort = {"vo.VIEW" , "vo.ADD_TO_CART" , "vo.PURCHASE"}, direction = Sort.Direction.DESC)Pageable pageable){
		
		
		PageRequest pageRequest = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Direction.fromString(StrUtil.emptyToDefault(request.getParameter("direction"), "DESC")) , StrUtil.emptyToDefault(request.getParameter("sort"), "vo.VIEW"));
		
		return monitorService.findProductMonitor(monitorQueryRequest, pageRequest);
	}
	
	@GetMapping("/product/summary")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop', 'affiliate')")
	public Map<String , Object> findProductMonitorSummary(MonitorQueryRequest monitorQueryRequest){
		
		return monitorService.findProductMonitorSummary(monitorQueryRequest);
	}
	
	@GetMapping("/product/export")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop')")
	public String exportProductMonitor(MonitorQueryRequest monitorQueryRequest , @PageableDefault(sort = {"vo.`VIEW`" , "vo.ADD_TO_CART" , "vo.PURCHASE"}, direction = Sort.Direction.DESC)Pageable pageable , HttpServletResponse response) throws IOException{
		
		Page<Map<String, Object>> monitors = monitorService.findProductMonitor(monitorQueryRequest, PageRequest.of(0, 100000, Sort.Direction.DESC, "vo.`VIEW`" , "vo.ADD_TO_CART" , "vo.PURCHASE"));
		
		List<Map<String, Object>> rows = new ArrayList<>();
		
		for (Map<String, Object> map : monitors.getContent()) {
			Map<String, Object> row = new LinkedHashMap<>();
			row.put("产品ID", map.get("product_id"));
			row.put("标题", map.get("title"));
			row.put("图片", map.get("image"));
			row.put("价格", map.get("price"));
			row.put("来源", map.get("source_type"));
			row.put("上架时间", map.get("up_at"));
			row.put("购物车删除", map.get("CART_DELETE"));
			row.put("点击", map.get("VIEW"));
			row.put("立即购买", map.get("BUY_NOW"));
			row.put("加购", map.get("ADD_TO_CART"));
			row.put("结算", map.get("PURCHASE"));
			row.put("点击转化", map.get("CLICK_RATE"));
			row.put("转化结算", map.get("PURCHASE_RATE"));
			row.put("点击结算", map.get("VIEW_PURCHASE_RATE"));
			row.put("同比购物车删除", map.get("CART_DELETE_MOM"));
			row.put("同比点击", map.get("VIEW_MOM"));
			row.put("同比立即购买", map.get("BUY_NOW_MOM"));
			row.put("同比加购", map.get("ADD_TO_CART_MOM"));
			row.put("同比结算", map.get("PURCHASE_MOM"));
			row.put("同比点击转化", map.get("CLICK_RATE_MOM"));
			row.put("同比转化结算", map.get("PURCHASE_RATE_MOM"));
			row.put("同比点击结算", map.get("VIEW_PURCHASE_RATE_MOM"));
			rows.add(row);
		}
		
		
		String filePath = CommonConstant.BASE_LOCATION + LocalDateTime.now(ZoneId.of("+8")).format(CommonConstant.DATETIMEFORMATTRT_DATE) + '/' + LocalDateTime.now(ZoneId.of("+8")).format(CommonConstant.DATETIMEFORMATTRT_TIME) + "-商品监测.xlsx" ; 
		ExcelWriter writer = ExcelUtil.getWriter(filePath);
		writer.write(rows, true);
		writer.close();
		return filePath ;
	}
	
	@GetMapping("/page")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop', 'affiliate')")
	public Page<Map<String , Object>> findPageMonitor(MonitorQueryRequest monitorQueryRequest , @PageableDefault(sort = {"show_count"}, direction = Sort.Direction.DESC)Pageable pageable){
		
		return monitorService.findPageMonitor(monitorQueryRequest, pageable);
	}
	
	@GetMapping("/event")
	@PreAuthorize("hasAnyRoles('admin','analysis', 'shop', 'affiliate')")
	public Page<Map<String , Object>> findEventMonitor(MonitorQueryRequest monitorQueryRequest , @PageableDefault(sort = {"click_count"}, direction = Sort.Direction.DESC)Pageable pageable){
		
		return monitorService.findEventMonitor(monitorQueryRequest, pageable);
	}
}
